﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

///<summary>
///
///</summary>
public class EndView : MonoBehaviour
{
    new Animation animation;

    //再玩一次
    public UnityAction Again;
    //商店委托
    public UnityAction OpenShop;

    //End
    GameObject endPanel;
    //总金币
    GameObject txtTotalMoney;
    //最高分数
    GameObject txtBestScore;
    //本局分数
    GameObject txtCurScore;
    //bg
    GameObject endBg;
    //再次游戏
    GameObject BtnAgain;
    //面板位置
    Vector2 endPos;
    //面板game中的位置;
    Vector2 endGamePos;
    //面板父类
    GameObject panelParent;
    //开启商店按钮
    GameObject btnShop;

    //面板在中间的位置
    Vector2 panelInGamePos;
    public void Init()
    {
        endPanel = GameObject.Find("Canvas/Ending");
        txtTotalMoney = GameObject.Find("Canvas/Ending/txtTotalMoney");
        txtBestScore = GameObject.Find("Canvas/Ending/txtBestScore");
        txtCurScore = GameObject.Find("Canvas/Ending/txtCurScore");
        panelParent = GameObject.Find("Canvas/Panel");
        endBg = panelParent.transform.Find("endBg").gameObject;
        BtnAgain = GameObject.Find("Canvas/Ending/BtnAgain");
        btnShop = GameObject.Find("Canvas/Ending/BtnShop");
        endPos = endPanel.transform.position;
        animation = GetComponent<Animation>();
        OpenPanel();
        ShowScore();

        BtnAgain.GetComponent<Button>().onClick.AddListener(() =>
        {
            Again();
			PlaySound("Sounds/Panel/click_start");

		});
        btnShop.GetComponent<Button>().onClick.AddListener(() =>
        {
            OpenShop();
			PlaySound("Sounds/Panel/click_start");
		});
    }
	void PlaySound(string path)
	{
		AudioManager.GetInstance.Play(path);
	}
	public void ShopReturnEnd()
    {
        panelInGamePos = new Vector2(0, 0);
        panelInGamePos = Camera.main.WorldToScreenPoint(panelInGamePos);
        gameObject.transform.position = panelInGamePos;
    }

    public void OpenPanel()
    {
        animation.Play();
        endBg.SetActive(true);
    }

    public void ClosePanel()
    {
        gameObject.transform.position = endPos;
    }

    public void RestGame()
    {
        ClosePanel();
        PlayerDataManager.GetIntance().Clear();
        endBg.SetActive(false);
        //文本复原
        txtCurScore.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetPoint().ToString();
        if (GameObject.FindGameObjectsWithTag("Balloon").Length > 0)
        {
            GameObject[] balloons = GameObject.FindGameObjectsWithTag("Balloon");
            for (int i = 0; i < balloons.Length; i++)
            {
                balloons[i].GetComponent<BalloonInputKeyComponent>().RestGame();
            }
        }
    }

    public void ShowScore()
    {
        PlayerDataManager.GetIntance().ScoreClose();
        txtCurScore.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetPoint().ToString();
        txtBestScore.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetBestScore().ToString();
        txtTotalMoney.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetTotalMoney().ToString();
    }
   
}
